﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Jy123.Web.Manage
{
    public partial class StarType : MgConfig
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("StarType.aspx");
            if (!Page.IsPostBack)
            {
                LoadInitialValue();
            }
        }

        protected void Repeater_DataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Jy123.Model.Public_StarType starType = e.Item.DataItem as Jy123.Model.Public_StarType;
                HiddenField idHidden = e.Item.FindControl("idHidden") as HiddenField;
                idHidden.Value = starType.ID.ToString();
                Label nameLabel = e.Item.FindControl("starTypeNameLabel") as Label;
                nameLabel.Text = starType.SName;
            }
        }

        protected void Repeater_Command(object sender, RepeaterCommandEventArgs e)
        {
            RepeaterItem item = e.Item as RepeaterItem;
            HiddenField hidden = e.Item.FindControl("idHidden") as HiddenField;
            int id = int.Parse(hidden.Value);
            Jy123.BLL.Public_StarType BLLStarType = new Jy123.BLL.Public_StarType();
            if (e.CommandName.Equals("Edit"))
            {
                Jy123.Model.Public_StarType starType = BLLStarType.GetModel(id);
                actionTypeHidden.Value = "Update";
                idHidden.Value = id.ToString();
                starTypeNameTextBox.Text = starType.SName;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                BLLStarType.Delete(id);
                LoadInitialValue();
                ClearPage();
            }
        }

        protected void Save(object sender, EventArgs e)
        {
            Jy123.BLL.Public_StarType BLLStarType = new Jy123.BLL.Public_StarType();
            if (actionTypeHidden.Value.Equals("Add"))
            {
                Jy123.Model.Public_StarType starType = new Jy123.Model.Public_StarType();
                starType.SName = starTypeNameTextBox.Text;
                BLLStarType.Add(starType);
            }
            else
            {
                int id = int.Parse(idHidden.Value);
                Jy123.Model.Public_StarType starType = BLLStarType.GetModel(id);
                starType.SName = starTypeNameTextBox.Text;
                BLLStarType.Update(starType);
            }
            LoadInitialValue();
            ClearPage();
        }

        private void ClearPage()
        {
            actionTypeHidden.Value = "Add";
            starTypeNameTextBox.Text = string.Empty;
        }

        private void LoadInitialValue()
        {
            Jy123.BLL.Public_StarType BLLStarType = new Jy123.BLL.Public_StarType();
            List<Jy123.Model.Public_StarType> liststarType = BLLStarType.GetModelList(string.Empty);
            starTypeRepeater.DataSource = liststarType;
            starTypeRepeater.DataBind();
            starTypeRepeater.Visible = liststarType.Count > 0;
        }
    }
}
